package com.derbysoft.dswitch.adapter.ihg.web.action.monitor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.concurrent.*;

public class FutureTaskCreator<E> {
    private static Log logger = LogFactory.getLog(FutureTaskCreator.class);
    public E create(Callable<E> task, long timeOut) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        FutureTask<E> futureTask = new FutureTask<E>(task);
        executorService.execute(futureTask);
        try {
            return futureTask.get(timeOut, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            futureTask.cancel(true);
            logger.error(e.getMessage(), e);
        } finally {
            executorService.shutdown();
        }
        return null;
    }
}
